#include "Enemy.h"
#include <iostream>
#include <GL/glut.h>
#include <vector>
#include "Object3D.h"
#include "Projectile.h"
#include "Vector3.h"
#include <cmath>

using namespace std;

const int Enemy::DYING_TIME = 200;


void Enemy::onAnimationFinish(int state)
{

	if (state == kFallingBackDying || state == kFallingForwardDying || state == kFallingBackSlowDying)
	{
		cout << "onfinish called" << endl;
		// or if dead bool is true
		dying = true;

	}

}

Enemy::Enemy() : moveSpeed(15), deathCounter(DYING_TIME)
{
	//lastState = 0;
	currentPlatform = NULL;
	facingRight = true;
	inAir = false;

	type = ENEMY;
	cw = 15;
	ch = 25;
	offsetX = -50;
	mModel = new Object3D(2, this);
	
	
	if (mModel->LoadModel("Models/Ogro/tris.md2","Models/Ogro/ogrobase.pcx") &&
		mModel->LoadModel("Models/Ogro/weapon.md2","Models/Ogro/weapon.pcx") )
	{
	
	//if (mModel->LoadModel("Models/martian/TRIS.MD2","Models/martian/MARTIAN.pcx") &&
	//	mModel->LoadModel("Models/martian/Weapon.MD2","Models/martian/Weapon.pcx") )
	//{
	}else
	{
		std::cout << "error loading model" << std::endl;
	}

	/*
	BMPClass bmp;
	BMPLoad("big_glow.bmp",bmp);
	
	glGenTextures(1, &glowTex);
	glBindTexture(GL_TEXTURE_2D, glowTex);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
	
	glTexImage2D(GL_TEXTURE_2D,0,3,bmp.width,bmp.height,0,GL_RGB,GL_UNSIGNED_BYTE,bmp.bytes);
	*/
}

Enemy::~Enemy()
{
	// Delete fired projectiles
	for (vector<Projectile*>::iterator it = projectiles.begin(); it != projectiles.end(); it++)
	{
		delete (*it);
	}
	delete mModel;
}



void Enemy::move(const std::vector<Collidable*>& plats)
{
	

	// Move fired projectiles
	for (vector<Projectile*>::iterator it = projectiles.begin(); it != projectiles.end();)
	{
		if ( (*it)->dead )
		{
			delete (*it);
			it = projectiles.erase(it);
		}else{
			(*it)->move(plats);
			it++;
		}
	}

	if (dying)
	{
		if (deathCounter > 0)
		{
			deathCounter--;
		}else{
			dead = true;
		}
		return;		// don't update model
	}
	

		position.x += xVel;
	
	Collidable* collision = checkCollision(plats);
	if (collision){
			position.x -= xVel;
			xVel = -xVel;
			facingRight = !facingRight;
	}

	
	yVel -= 0.6;//0.2;			// gravity
	position.y += yVel;
	collision = checkCollision(plats);
	if (collision){
		
		if (yVel < 0)		// On ground
		{
			inAir = false;
			currentPlatform = collision;
			if (xVel == 0)
			{
				mModel->SetState(kRunning,true);
				xVel = moveSpeed;
			}
		}

		float ledgeLeft = currentPlatform->position.x  - currentPlatform->cw;
		float ledgeRight = currentPlatform->position.x  + currentPlatform->cw;

		if ( (position.x - (sx * cw)) < ledgeLeft )
		{
			position.x += moveSpeed;
			xVel = moveSpeed;
			facingRight = true;
		}
	
		if ( (position.x + (sx * cw)) > ledgeRight )
		{
			position.x -= moveSpeed;
			xVel = -moveSpeed;
			facingRight = false;
		}

		/*
		if ( (position.x - (sx * cw)) < ledgeLeft || (position.x + (sx * cw) > ledgeRight) )
		{
			cout << "LEDGE!" << endl;
			position.x -= xVel;
			xVel = -xVel;
			facingRight = !facingRight;
		}*/
		position.y -= yVel;
		yVel = 0;
	}else{
		inAir = true;
	}

	//if (!inAir)
	if ( !inAir || !mModel->repeat )
	{
		mModel->Update();
	}
	
}

void Enemy::draw()
{
	

	//GLfloat diff[4] = {0,1,1,1};
	GLfloat spec[4] = {1,1,1,1};

	int shininess = 128;
	//glMaterialfv(GL_FRONT,GL_DIFFUSE, diff);
	glMaterialfv(GL_FRONT,GL_SPECULAR, spec);

	glMateriali(GL_FRONT,GL_SHININESS, shininess);
	
	
	glEnable(GL_TEXTURE_2D);
	//glDisable(GL_TEXTURE_2D);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	
	glTranslatef(position.x + offsetX, position.y + offsetY, 0);
	
	glScalef(sx,sy,sz);
	//glutSolidCube(cw * 2);
	

	if (!facingRight)
	{
		glRotatef(-180,0,1,0);
	}
	glRotatef(-90,1,0,0);
	
	mModel->Draw();

	glPopMatrix();

	glDisable(GL_TEXTURE_2D);


	// Draw fired projectiles
	for (vector<Projectile*>::iterator it = projectiles.begin(); it != projectiles.end(); it++)
	{
		(*it)->draw();
	}
	
}

void Enemy::youreDead(float pVel)
{
	// don't worry about collisions
	//cw = 0;
	//ch = 0;

	//mModel->SetState(kFallingBackSlowDying,false);
	mModel->SetState(kFallingForwardDying,false);
	//onAnimationFinish(kFallingForwardDying);

	/*
		if (pVel < 0)
		{
			if (facingRight)
			{
				mModel->SetState(kFallingBackDying,false);
			}else{
				mModel->SetState(kFallingForwardDying,false);
			}
		}else{
			if (facingRight)
			{
				mModel->SetState(kFallingForwardDying,false);
			}else{
				mModel->SetState(kFallingBackDying,false);
			}
		}*/
	

}

void Enemy::setMoveSpeed(float ms)
{
	moveSpeed = ms;
}